package sk.stuba.fiit.pki.ws.service.impl;


import org.bouncycastle.tsp.TimeStampRequest;
import org.bouncycastle.tsp.TimeStampResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ws.server.endpoint.annotation.Endpoint;
import org.springframework.ws.server.endpoint.annotation.PayloadRoot;

import sk.stuba.fiit.pki.service.PkiService;
import sk.stuba.fiit.pki.ws.schema.beans.TimestampRequest;
import sk.stuba.fiit.pki.ws.schema.beans.TimestampResponse;
import sk.stuba.fiit.pki.ws.service.CommUtil;
import sk.stuba.fiit.pki.ws.service.MarshallingTimeStampService;

/**
 * TimeStamp service endpoint.
 * 
 * @author Marek Hruska
 */
@Endpoint
public class TimeStampEndpoint implements MarshallingTimeStampService {
    
	@Autowired
	PkiService pkiService;
	
    /**
     * Gets timestamp for data.
     */
    @PayloadRoot(localPart=TIMESTAMP_REQUEST, namespace=NAMESPACE)
	public TimestampResponse getTimeStamp(TimestampRequest request) {
      		
    	TimeStampRequest tsRequest;
		try {
			tsRequest = new TimeStampRequest(CommUtil.getInstance().codeBase64StringToByte(request.getTimestamreq().get(0)));
			TimeStampResponse response;
			response = pkiService.generateTimeStamp(tsRequest);
			return new TimestampResponse().withTimestamresp(CommUtil.getInstance().codeBase64ByteToString(response.getEncoded()));
		} catch (Exception e) {
			e.printStackTrace();
		} 
		return new TimestampResponse();   	
	}
}
